package com.hmall.item.service.impl;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.Filters;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;


import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private  ItemMapper itemMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;




    @Override
    public PageDTO<Item> selectPage(Integer page, Integer size) {

        Page<Item> itemPage = new Page<>(page,size);
        itemMapper.selectPage(itemPage,null);
        PageDTO<Item> itemPageDTO = new PageDTO<>(itemPage.getTotal(),itemPage.getRecords());
        return  itemPageDTO;


    }
    /**
     *
     * 上架下架商品
     * **/

    @Override
    public void status(Long id, Integer status) {
     Item item = new Item();
     item.setId(id);
     item.setStatus(status);
     itemMapper.updateById(item);
         //上下架商品，需要及时更新缓存中的热销商品列表
        rabbitTemplate.convertAndSend("top20.queue","更新热销数据");
    }
    /***
     *
     * 基本搜索功能实现
     *
     * */

    @Override
    public PageDTO<Item> finds(Filters filters) {
    //1.将TOP20存入redis
        //1.1先判断是否为null,分类(sortBy)是否等于有的类别(default),key是否为"",如果都没有存入redis
        if ("".equals(filters.getKey())&&"default".equals(filters.getSortBy())&&filters.getCategory() == null&&filters.getBrand() == null&&filters.getMaxPrice() == null){
        //1.2定义redis的key
            String top20key = "Top20";
            //1.3获取springBoot与redis交互，获取redis中的key和value
            String json = redisTemplate.opsForValue().get(top20key);
            //1.4将json转为java列表
            List<Item> items = JSONUtil.toList(json, Item.class);
            return  new PageDTO<>(20L,items);
        }
        //2.查询
        Page<Item> itemPage = new Page<>(filters.getPage(), filters.getSize());
        //2.1构建分页查询条件
        LambdaQueryWrapper<Item> queryWrapper = Wrappers.<Item>lambdaQuery()
                .like(filters.getKey() != null,Item::getName,filters.getKey())//模糊查询
                .eq(filters.getBrand() != null,Item::getBrand,filters.getBrand())//等值查询
                .eq(filters.getCategory() != null,Item::getCategory,filters.getCategory())
                .between((filters.getMinPrice() != null && filters.getMaxPrice() != null),Item::getPrice,filters.getMinPrice(),filters.getMaxPrice())
                .orderByDesc(filters.getSortBy() != null,Item :: getSold)
                .orderByDesc(Item::getPrice);

        //3.分页
        itemMapper.selectPage(itemPage,queryWrapper);
        PageDTO<Item> itemPageDTO = new PageDTO<>(itemPage.getTotal(),itemPage.getRecords());
        return  itemPageDTO;
    }

    /**
     *
     * 查询Top20的商品
     * **/

    @Override
    public List<Item> getTop20() {
       List<Item> items = itemMapper.selectList(Wrappers.<Item>lambdaQuery()
               .orderByDesc(Item::getSold)
               .last("LIMIT 20"));
       return  items;

    }
}
